# -*- coding: utf-8 -*-
#
# Copyright (C) 2011  Benjamin Heil
#
# This file is part of Feedmail.
#
# Feedmail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.conf import settings
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from account.models import *

def add_testuser(c, username, password, mail):
    # Register a new user
    url = reverse('account_register')
    response = c.client.get(url)
    c.assertEqual(response.status_code, 200)

    register_data = {
        'username': username,
        'password1': password,
        'password2': password,
        'email': mail,
    }
    response = c.client.post(url, register_data)
    c.assertEqual(response.status_code, 200)
    return activate_user(c, username)

def activate_user(c, username):
    # Activate the user
    user = User.objects.get(username=username)
    activation_id = AccountActivation.objects.get(user=user)
    url = reverse('account_activate', args=[activation_id.activation_id])
    response = c.client.get(url)
    c.assertEqual(response.status_code, 200)
    return user

class TestAccountApp(TestCase):

    def _check_redirect_for_login_required_page(self, url, redirect_url):
        response = self.client.get(url)
        self.assertRedirects(response, redirect_url)

    def test_profile(self):
        url = reverse('account_profile')
        add_testuser(self, username='testprofile', password='testpassword', mail='test@bheil.net')
        self._check_redirect_for_login_required_page(url, 'http://testserver/account/login.html?next=/account/profile.html')

        login = self.client.login(username='testprofile', password='testpassword')
        self.failUnless(login, 'Could not log in')

        # Request testprofile's profile page
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['user'].username, 'testprofile')

        # Change username
        response = self.client.post(url, {'username': 'testprofile2', 'email': 'test@bheil.net'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['user'].username, 'testprofile')

        self.client.logout()
        self._check_redirect_for_login_required_page(url, 'http://testserver/account/login.html?next=/account/profile.html')
        login = self.client.login(username='testprofile2', password='testpassword')
        self.failUnless(login, 'Could not log in')

        # Change mail and activate again
        response = self.client.post(url, {'username': 'testprofile2', 'email': 'test2@bheil.net'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['user'].username, 'testprofile2')
        activate_user(self, 'testprofile2')

        self.client.logout()
        self._check_redirect_for_login_required_page(url, 'http://testserver/account/login.html?next=/account/profile.html')

    def test_delete_account(self):
        if not settings.ACCOUNT_CAN_DELETE:
            return
        url = reverse('account_delete_my_account')
        add_testuser(self, username='testdelete', password='testpassword', mail='testdelete@bheil.net')
        self._check_redirect_for_login_required_page(url, 'http://testserver/account/login.html?next=/account/delete-my-account.html')

        login = self.client.login(username='testdelete', password='testpassword')
        self.failUnless(login, 'Could not log in')

        # get confirmation form
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        response = self.client.post(url, {'confirmation': True})
        self.assertEqual(response.status_code, 200)

        try:
            User.objects.get(username='testdelete')
            self.fail('user "testdelete" was not deleted')
        except User.DoesNotExist:
            pass
